package com.jic.market.impl.rest.discountamount.model;

import com.jic.market.common.util.Money;
import com.jic.market.enums.DiscountDetailStatusEnum;
import com.jic.market.enums.FrontPromotionTypeEnum;
import com.jic.market.impl.rest.discountamount.po.GoodsPo;
import lombok.Data;
import org.springframework.context.annotation.Scope;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;

/**
 * @author yinhaijun
 * @date: 2020/4/7
 */
@Scope("prototype")
@Data
public class PromotionAmountModel implements Serializable {
    private static final long serialVersionUID = -2859482493431972767L;

    private Long id;
    /** ---------源数据--------- */
    /** 活动id */
    private String promotionId;
    /** 活动code */
    private String promotionCode;
    /** 活动类型 */
    private FrontPromotionTypeEnum frontPromotionTypeEnum;
    /** 包括当前商品参与本次活动的总额，用于计算一些影响总价的促销活动，注意该值可能随着计算变动 */
    private BigDecimal joinTotalAmount;


    /** -----------规则数据-------------- */
    /** 优惠劵专用 */
    private List<String> couponCodeList;

    /** 是否叠加应用，次数，-1否，n次数，叠加型活动专用,如满额 */
    private Integer overlayAppNum;
    /** 是否叠加应用,0否，1是, */
    private Boolean overlayAppFlag;

    /** -----------暂存数据-------------- */
    private Object ruleData;


    private DiscountDetailStatusEnum discountDetailStatus;


    /** -----------计算结果数据------------- */
    /** 优惠单价 */
    private BigDecimal promotionDiscountSingleAmount;
    /** 优惠总价 */
    private BigDecimal promotionDiscountTotalAmount;
    /** 促销单价 */
    private BigDecimal promotionAvailableSinglePrice;
    /** 促销总价 */
    private BigDecimal promotionAvailableTotalPrice;

    /** 优惠总价为0时候，是否删除此模型 */
    private Boolean deleteModel = true;

//    public void fillModelBySpecialPrice(GoodsPo goodsPo){
//        // 促销单价 = 特价
//        this.promotionAvailableSinglePrice = new BigDecimal(this.specialPrice.doubleValue());
//        fillPromotionAvailableSinglePrice(goodsPo);
//    }
//
//    public void fillModelByFallAmount(GoodsPo goodsPo){
//        // 促销单价 = 特价
//        this.promotionAvailableSinglePrice = Money.subtract(goodsPo.getMarketPrice(), this.fallAmount);
//        fillPromotionAvailableSinglePrice(goodsPo);
//    }

//    public void fillModelByDiscountScale(BigDecimal discountScale, GoodsPo goodsPo) {
//        // 促销单价 = 原价 * 折扣
//        BigDecimal promotionAvailableSinglePrice = Money.multiply(goodsPo.getMarketPrice(), discountScale);
//        fillModelByPromotionAvailableSinglePrice(promotionAvailableSinglePrice, goodsPo);
//    }

//    /** 按优惠单价 */
//    public void fillModelByPromotionDiscountSingleAmount(BigDecimal promotionDiscountSingleAmount, GoodsPo goodsPo) {
//        // 优惠单价
//        this.promotionDiscountSingleAmount = promotionDiscountSingleAmount;
//        // 优惠总价 = 优惠单价 * 数量
//        this.promotionDiscountTotalAmount =  Money.multiply(this.promotionDiscountSingleAmount, new BigDecimal(goodsPo.getNum()));
//        // 促销总价 = 商品总价 - 优惠总价
//        this.promotionAvailableTotalPrice = Money.subtract(goodsPo.getTotalAmount(), this.promotionDiscountTotalAmount);
//        // 促销单价 = 促销总价 / 数量
//        this.promotionAvailableSinglePrice = Money.divide(this.promotionAvailableTotalPrice, new BigDecimal(goodsPo.getNum()));
//    }

    /** 按优惠总价 */
    public void fillModelByPromotionDiscountTotalAmount(BigDecimal promotionDiscountTotalAmount, GoodsPo goodsPo) {
        // 注意：此时的优惠总价不是我们要的优惠总价，必须按照参与总价进行比例分配
        // 比例 = 当前商品总额 / 参与本次活动的所有商品总额
        // 优惠总价 = 传入的优惠总价 * 比例
        BigDecimal bili = Money.divide(goodsPo.getTotalAmount(), this.joinTotalAmount);
        // 优惠总价
        this.promotionDiscountTotalAmount = Money.multiply(promotionDiscountTotalAmount, bili);
        // 优惠单价 = 优惠总价 / 数量
        this.promotionDiscountSingleAmount =  Money.divide(this.promotionDiscountTotalAmount, new BigDecimal(goodsPo.getNum()));
        // 促销总价 = 商品总价 - 优惠总价
        this.promotionAvailableTotalPrice = Money.subtract(goodsPo.getTotalAmount(), this.promotionDiscountTotalAmount);
        // 促销单价 = 促销总价 / 数量
        this.promotionAvailableSinglePrice = Money.divide(this.promotionAvailableTotalPrice, new BigDecimal(goodsPo.getNum()));
    }

    /** 按促销单价 */
    public void fillModelByPromotionAvailableSinglePrice(BigDecimal promotionAvailableSinglePrice, GoodsPo goodsPo) {
        // 促销单价
        this.promotionAvailableSinglePrice = promotionAvailableSinglePrice;
        // 促销总价 = 促销单价 * 数量
        this.promotionAvailableTotalPrice = Money.multiply(this.promotionAvailableSinglePrice, new BigDecimal(goodsPo.getNum()));
        // 优惠单价 = 原价 - 促销单价
        this.promotionDiscountSingleAmount = Money.subtract(goodsPo.getMarketPrice(), this.promotionAvailableSinglePrice);
        // 优惠总价 = 优惠单价 * 数量
        this.promotionDiscountTotalAmount = Money.multiply(this.promotionDiscountSingleAmount, new BigDecimal(goodsPo.getNum()));
    }





    /** 不优惠处理 */
    public void fillNoDiscount(GoodsPo goodsPo) {
        // 促销单价 = 原价
        this.promotionAvailableSinglePrice = new BigDecimal(goodsPo.getMarketPrice().doubleValue());
        fillModelByPromotionAvailableSinglePrice(promotionAvailableSinglePrice, goodsPo);
    }
//
//    /** 全额优惠处理，白送 */
//    public void fillFullDiscount(GoodsPo goodsPo) {
//        // 促销单价 = 0
//        this.promotionAvailableSinglePrice = new BigDecimal(0);
//        fillPromotionAvailableSinglePrice(goodsPo);
//    }
//
//
//
//    public void fillPromotionAvailableSinglePrice(GoodsPo goodsPo) {
//        // 优惠单价 = 原价 - 促销单价
//        this.promotionDiscountSingleAmount = Money.subtract(goodsPo.getMarketPrice(), this.promotionAvailableSinglePrice);
//        if(promotionDiscountSingleAmount.compareTo(BigDecimal.ZERO) == -1){
//            throw new MarketException(MarketErrorCodeEnum.PARAM_ERROR, "优惠金额为负数" + "promotionId=" + this.promotionId + "skuId=" + goodsPo.getSkuId());
//        }
//        this.promotionDiscountTotalAmount = Money.multiply(promotionDiscountSingleAmount, new BigDecimal(goodsPo.getNum()));
//
//    }

}
